package com.hqs.listener.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hqs.common.utils.SharedPreferenceUtil;
import com.hqs.listener.R;
import com.hqs.listener.activity.MainActivity;
import com.hqs.listener.service.MusicService;
import com.hqs.listener.utils.TimeUtil;
import com.hqs.listener.view.RoundTextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by super on 2017/11/13.
 */

public class TimerFragment extends BaseFragment {


    @BindView(R.id.tv1)
    RoundTextView tv1;

    @BindView(R.id.tv2)
    RoundTextView tv2;

    @BindView(R.id.tv3)
    RoundTextView tv3;

    @BindView(R.id.tv4)
    RoundTextView tv4;

    @BindView(R.id.tv5)
    RoundTextView tv5;

    @BindView(R.id.tv6)
    RoundTextView tv6;

    @BindView(R.id.tv7)
    RoundTextView tv7;

    @BindView(R.id.tv8)
    RoundTextView tv8;

    ArrayList<RoundTextView> tvs;

    @BindView(R.id.tvTime1)
    TextView tvTime;

    int totalTime = 0;

    Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_timer, null);

        ButterKnife.bind(this, view);
        setup();
        return view;
    }

    void setup(){

        tv1.value = 10 * 60;
        tv2.value = 20 * 60;
        tv3.value = 20 * 60;
        tv4.value = 30 * 60;
        tv5.value = 90 * 60;
        tv6.value = 60 * 60;
        tv7.value = 120 * 60;
        tv8.value = 180 * 60;

        tvs = new ArrayList<>();
        tvs.add(tv1);
        tvs.add(tv2);
        tvs.add(tv3);
        tvs.add(tv4);
        tvs.add(tv5);
        tvs.add(tv6);
        tvs.add(tv7);
        tvs.add(tv8);

        RoundTextView.OnSelectedStateChangedListener listener = new RoundTextView.OnSelectedStateChangedListener() {
            @Override
            public void onSelectedStateChanged(boolean selected, int value) {
                totalTime = 0;

                int i = 0;
                for (RoundTextView textView : tvs){
                    if (textView.selected) {
                        totalTime += textView.value;
                    }
                    SharedPreferenceUtil.set("round_text_view_" + i, textView.selected);
                    i += 1;
                }

                if (totalTime == 0){
                    tvTime.setText("--时--分--秒");
                    service.stopTimer();
                }
                else {
                    tvTime.setText(TimeUtil.timeOf(totalTime));
                    service.setTime(totalTime);
                }
            }
        };
        for (RoundTextView textView : tvs){
            textView.setOnSelectedStateChangedListener(listener);
        }
    }

    @Override
    public void didSetService() {

        handler.post(new Runnable() {
            @Override
            public void run() {
                if (service.getTimerThread() != null) {
                    int i = 0;
                    for (RoundTextView textView : tvs){
                        boolean selected = SharedPreferenceUtil.get("round_text_view_" + i, false);
                        textView.setSelected(selected);
                        i += 1;
                    }
                }
                else {
                    for (RoundTextView textView: tvs){
                        textView.setSelected(false);
                    }
                }
            }
        });


        service.setTimeChangedListener(new MusicService.TimeChangedListener() {
            @Override
            public void timeChanged(final int sec) {

                if (MainActivity.instance.viewPager.getCurrentItem() == 2) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (sec == 0){
                                tvTime.setText("--时--分--秒");
                                for (RoundTextView textView: tvs){
                                    textView.setSelected(false);
                                }
                                totalTime = 0;
                            }
                            else {
                                tvTime.setText(TimeUtil.timeOf(sec));
                            }
                        }
                    });
                }
            }
        });
    }
}
